package com.guigui.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigui.common.utils.PageUtils;
import com.guigui.common.utils.Query;
import com.guigui.gulimall.product.dao.AttrGroupDao;
import com.guigui.gulimall.product.entity.AttrEntity;
import com.guigui.gulimall.product.entity.AttrGroupEntity;
import com.guigui.gulimall.product.entity.CategoryEntity;
import com.guigui.gulimall.product.service.AttrGroupService;
import com.guigui.gulimall.product.service.AttrService;
import com.guigui.gulimall.product.service.CategoryService;
import com.guigui.gulimall.product.vo.BrandWithAttrVo;
import com.guigui.gulimall.product.vo.web.SpuAttrGroupVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过catid查询分组参数信息
     *
     * @return: [java.util.Map<java.lang.String, java.lang.Object>, java.lang.Long]
     * @Author: xin
     * @Date: 2021/7/28 14:12
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        //catId == 0 即 key为空
        //模糊查询判断，wrapper添加判断条件
        if (!StringUtils.isEmpty(key)) {
            //select * from pms_attr_group where catelog_id = catId and (attr_group_name like '%key%' or descript like '%key%')
            wrapper.and((obj) -> {
                obj.like("attr_group_name", key).or().like("descript", key);
            });
        }
        //catId默认值情况，返回所有结果
        IPage<AttrGroupEntity> page = null;
        if (catId == 0) {
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
        } else {
            page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper.eq("catelog_id", catId));
        }


        return new PageUtils(page);
    }

    @Override
    public AttrGroupEntity getInfoById(Long attrGroupId) {
        AttrGroupEntity byId = this.getById(attrGroupId);
        byId.setCatelogIds(new ArrayList<>(3));
        findCategories(byId.getCatelogId(),byId.getCatelogIds());
        return byId;
    }

    @Override
    public List<BrandWithAttrVo> getBrandWithAttrByCatelogId(Long catelogId) {

        // catalogId查出来的属性分组
        List<AttrGroupEntity> attrGroupEntityList = this.list(
                new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)
        );

        List<BrandWithAttrVo> brandWithAttrVos = attrGroupEntityList.stream().map(e -> {
            BrandWithAttrVo brandWithAttrVo = new BrandWithAttrVo();
            // 基本属性对拷
            BeanUtils.copyProperties(e, brandWithAttrVo);

            List<AttrEntity> attrRelation = attrService.getAttrRelation(e.getAttrGroupId());

            brandWithAttrVo.setAttrs(attrRelation);

            return brandWithAttrVo;
        }).collect(Collectors.toList());


        return brandWithAttrVos;
    }

    @Override
    public List<SpuAttrGroupVo> getBrandWithAttrBySpuId(Long spuId, Long catalogId) {
        return baseMapper.getBrandWithAttrBySpuId(spuId, catalogId);
    }


    public void findCategories(Long catelogId, List<Long> list){
        CategoryEntity byId = categoryService.getById(catelogId);
        //是否有父分类
        if (byId.getParentCid() != 0){
            findCategories(byId.getParentCid(), list);
        }

        list.add(byId.getCatId());

    }

}